package com.wsd.web;

import com.alibaba.fastjson.JSONObject;
import com.cif.domain.Customer;
import com.cif.service.CustomerService;
import com.wsd.domain.NormalLoan;
import com.wsd.domain.OverdueLoan;
import com.wsd.service.LOverdueLoanService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname LOverdueLoanController
 * @Description TODO
 * @Date 2020/7/20 11:05
 * @Created by niuxiaojuan
 */
@RestController
@RequestMapping("/manage")
public class LOverdueLoanController extends BaseController{
    @Autowired
    LOverdueLoanService lOverdueLoanService;

    @Reference(version = "1.0.0", url = "${dubbo.service.cifurl}")
    private CustomerService customerService;

    @RequestMapping("/getLOverLoanCount")
    public Map LOverdueLoanCount()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BigDecimal loanMoney = lOverdueLoanService.sumLOverdueLoan();
            BigDecimal repayLoanMoney = lOverdueLoanService.sumRepayLoanMoney();
            BigDecimal interest = lOverdueLoanService.findLOverInterest();//长期贷款应收总利息
            BigDecimal repayInterest = lOverdueLoanService.sumRepayInterest();
            BigDecimal overdueMoney = lOverdueLoanService.sumLOverdueMoney();
            BigDecimal overdueInterest = lOverdueLoanService.sumLOverdueInterest();

            map.put("loanMoney",loanMoney);
            map.put("repayLoanMoney",repayLoanMoney);
            map.put("interest",interest);
            map.put("repayInterest",repayInterest);
            map.put("overdueMoney",overdueMoney);
            map.put("overdueInterest",overdueInterest);

            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
        }catch (Exception e)
        {
            map.put("success","false");
            map.put("resultCode", "LOANCOUNT-ERROR");
            map.put("resultMsg","返回长期逾期贷款统计信息出错");
        }

        return map;
    }

    @RequestMapping("/getLOverLoanCountList")
    public Map getLOverLoanCountList(@RequestParam("pagenum") int pagenum, @RequestParam("num") int num)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try{
        List<OverdueLoan> list=lOverdueLoanService.findLOverLoanForm(pagenum,num);
        for(OverdueLoan l:list)
        {
            com.cif.domain.Customer c = JSONObject.parseObject(JSONObject.toJSONString(changeResult(customerService.getCustomerById(l.getUserId().toString())).getData()), Customer.class);
            l.setUserName(c.getUserName());
            l.setIdNumber(c.getIdNumber());
        }
            map.put("success","true");
            map.put("resultCode", "000");
            map.put("resultMsg", "OK");
            map.put("recordCount",lOverdueLoanService.countLOverLoanForm());
            map.put("list", list);
    }catch(Exception e)
    {
        map.put("success","false");
        map.put("resultCode", "GETOVERDUELOAN-ERROR");
        map.put("resultMsg","返回长期逾期贷款信息出错");
    }
        return map;
    }
}
